package com.fare.taluo.util.excel.util;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.fare.taluo.util.excel.engine.module.ExcelCellFormat;
import com.fare.taluo.util.excel.engine.module.PropertyModel;
import com.fare.taluo.util.excel.engine.module.SheetModel;

import jxl.write.Blank;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;

public class ExcelOperationUtils {

	/**
	 * 将value转化为需要显示的类型
	 * @param c 单元格列号
	 * @param r 单元格行号
	 * @param value
	 * @param propertyModel
	 * @return
	 * @throws Exception
	 */
	public static WritableCell getPropertyValue(int c, int r, Object value,
			PropertyModel propertyModel) throws Exception {
		if (propertyModel.getType() != null) {
			return getValueByType(c, r, value, propertyModel);
		} else {
			return getValueByReflect(c, r, value, propertyModel);
		}

	}

	/**
	 * 需要配置property元素的type属性
	 * @param c
	 * @param r
	 * @param value
	 * @param propertyModel
	 * @return
	 * @throws Exception
	 */
	public static WritableCell getValueByType(int c, int r, Object value,
			PropertyModel propertyModel) throws Exception {
		String type = propertyModel.getType();
		String format = propertyModel.getFormat();
		String result = "";
		if (value == null || value.equals(""))
			result = "";
		if (type.equalsIgnoreCase("Integer")) {
			result = ((Integer) value).toString();
		}
		if (type.equalsIgnoreCase("Date")) {
			result = parseDate2String((Date) value, format);
		}
		if (type.equalsIgnoreCase("Float")) {
			result = ((Float) value).toString();
		}
		if (type.equalsIgnoreCase("Double")) {
			result = ((Double) value).toString();
		}
		if (type.equalsIgnoreCase("String")) {
			result = ((String) value).toString();
		}
		if (type.equalsIgnoreCase("Boolean")) {
			result = ((Boolean) value).toString();
		}
		if (type.equalsIgnoreCase("Long")) {
			result = ((Long) value).toString();
		}
		if (type.equalsIgnoreCase("Short")) {
			result = ((Short) value).toString();
		}
		if (type.equalsIgnoreCase("BigDecimal")) {
			result = ((BigDecimal) value).toString();
		}
		return new Label(c, r, result.toString());

	}

	/**
	 * 通过类反射得到bean的属性，不需要配置property元素的type属性
	 * @param c
	 * @param r
	 * @param value
	 * @param propertyModel
	 * @return
	 * @throws Exception
	 */
	public static WritableCell getValueByReflect(int c, int r, Object value,
			PropertyModel propertyModel) throws Exception {

		String format = propertyModel.getFormat();
		Map<String, String> map = propertyModel.getDataStyleMap();
		WritableCell cell = null;
		WritableCellFormat cellFormat = null;
		if (value == null || value.equals("")) {
			cell = new Blank(c, r);
			cellFormat = new ExcelCellFormat().getCellFormat(map);
			cell.setCellFormat(processCellFormat(c,r,propertyModel,cellFormat));
			return cell;
		}
		if (value instanceof String) {
			if (StringUtils.isNumber((String) value)) {// 是字符串型数据，但全为数字
				if (format != null) {
					jxl.write.NumberFormat nf = new jxl.write.NumberFormat(format);
					cellFormat = new ExcelCellFormat().getCellFormat(map,
							new jxl.write.WritableCellFormat(nf));
				}
				
				cell = new jxl.write.Number(c, r, Double
						.valueOf((String) value));
			} else {
				cell = new Label(c, r, value.toString());
			}
		} else if (value instanceof Number) {
			if (format != null) {
				jxl.write.NumberFormat nf = new jxl.write.NumberFormat(format);
				cellFormat = new jxl.write.WritableCellFormat(nf);
				cellFormat = new ExcelCellFormat().getCellFormat(map,
						new jxl.write.WritableCellFormat(nf));
			}			
			cell = new jxl.write.Number(c, r, ((Number) value).doubleValue());
		} else if (value instanceof Date) {
			cell = new Label(c, r, parseDate2String((Date) value, format));
		} else if (value instanceof Boolean) {
			cell = new jxl.write.Boolean(c, r, (Boolean) value);
		} else {
			cell = new Label(c, r, value.toString());
		}
		if (cellFormat == null) {
			cellFormat = new ExcelCellFormat().getCellFormat(map);
		}		
		cell.setCellFormat(processCellFormat(c,r,propertyModel,cellFormat));
		return cell;
	}
	
	/**
	 * 为了实现Excel的个性化显示需求，可以扩展实现接口IExcelCellDisplayRugular
	 * @param c 单元格所在的列
	 * @param r 单元格所在的行
	 * @param pm 
	 * @param cf
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private static WritableCellFormat processCellFormat(int c,int r,PropertyModel pm,WritableCellFormat cf){
		String temp = pm.getReguarMap().get("class");
		if(temp!=null){
			try {
				Class clazz = Class.forName(temp);
				Method  m =clazz.getMethod("processCell", int.class,int.class,Map.class,WritableCellFormat.class);
				return(WritableCellFormat) m.invoke(clazz.newInstance(), c,r,pm.getReguarMap(),cf);
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		
		return cf;
	}

	/**
	 * 取得Bean属性值
	 * 
	 * @param property
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public static Object getBeanPropertyValue(String property, Object bean)
			throws Exception {
		if (property.indexOf("$") != -1) {
			return StringUtils.getParameterProperty(property, bean);
		}
		Object obj = BeanUtil.getNestedProperty(bean, property);
		if (obj == null || obj.equals("")) {
			return "";
		}
		return obj;
	}

	/**
	 * 将日期按照某种格式转化为字符串
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String parseDate2String(Date date, String format) {
		if (date == null)
			return "";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			String dateStr = sdf.format(date);
			return dateStr;
		} catch (Exception e) {
			e.printStackTrace();
			//System.out.println("日期时间格式化错误");
			return null;
		}
	}

	

	
	/**
	 * 
	 * @param sheet
	 */
	public static void setSheetFormat(WritableSheet sheet,SheetModel sheetModel){
		Map<String,String> map= new HashMap<String ,String>();
		//setPropertyValue(Object bean, String property,Object value) 
		BeanUtil.setPropertyValue(sheet.getSettings(),"verticalFreeze",2);
//		sheet.getSettings().setVerticalFreeze(2);
//		sheet.getSettings().setHorizontalFreeze(2);
//		sheet.getSettings().setShowGridLines(false);
	}

	
	


	/**
	 * 将字符串按照规定的格式转化为日期类型
	 * @param value
	 * @param format
	 * @return
	 */
	public static Date parseString2Date(String value, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			Date date = sdf.parse(value);
			return date;
		} catch (Exception e) {
			e.printStackTrace();
			//System.out.println("日期时间格式化错误");
			return null;
		}
	}

}
